import os
from email.parser import Parser
from collections import Counter
import simplejson
rootdir = "/home/shzhuoyu/downloads/maildir/"

def email_analyse(inputfile,to_list):
    with open(inputfile, "r", encoding='utf-8') as f:
        try:
            data = f.read()
        except:
            with open(inputfile, "r", encoding='ISO-8859-1') as f:
                data = f.read()
    email = Parser().parsestr(data)
    if email['to']:
        email_to = email['to']
        email_to = email_to.replace("\n", "")
        email_to = email_to.replace("\t", "")
        email_to = email_to.replace(" ", "")
        email_to = email_to.split(",")
        for email_to_1 in email_to:
            to_list.append(email_to_1)


Names = os.listdir(rootdir)
reuslt = ''
for name in Names:
    to_list = []
    for directory, subdirectory, filenames in os.walk(rootdir+name):
        # print('now in: ' + directory)
        for filename in filenames:
            email_analyse(os.path.join(directory, filename), to_list)
    c = Counter(to_list)
    most = c.most_common(10)
    in_list = []
    for i in range(len(most)-1):
        if(most[i][1]>most[i+1][1]*2):
            in_list.append(most[i][0])
            break
        else:
            in_list.append(most[i][0])

    print(name+":\n",in_list)
    reuslt+=name+":\nHe/She might used "+str(len(in_list))+"mail adress\nThe mail address he/she might  used"+str(in_list)
    reuslt += "\n\n"

with open("ex3.txt", "w") as f:
    f.write(reuslt)
    f.write("\n")